﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Siren.MFE.Bll.SirenField;
using Siren.MFE.WebApplication.App_Code;

namespace Siren.MFE.WebApplication.UserControls
{
    public partial class LeftTree : BaseUserControl
    {
        private const char SEPERATOR = '|';

        private const int ID_INDEX = 0;
        private const int PARENTID_INDEX = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindTreeData();
            }
        }

        private void BindTreeData()
        {
            SirenFieldMgr mgr = new SirenFieldMgr(GetConnectionString);

            List<SirenFieldInfo> lstSF = mgr.GetParentSirenFields(1);

            PopulateTree(lstSF);

            //this.tvSirenField.Nodes[0].PopulateOnDemand = false;
            //this.tvSirenField.Nodes[0].SelectAction = TreeNodeSelectAction.Expand;

        }

        private void PopulateTree(List<SirenFieldInfo> lstSirenFields)
        {
            foreach (SirenFieldInfo sirenfield in lstSirenFields)
            { 
                TreeNode treeNode = new TreeNode(sirenfield.Caption, sirenfield.ID);
                
                this.tvSirenField.Nodes.Add(treeNode);

                if (sirenfield.ChildNodes.Count > 0)
                {
                    treeNode.SelectAction = TreeNodeSelectAction.Expand;
                    treeNode.PopulateOnDemand = true;
                    //treeNode.Collapse();

                    PopulateChildNodes(treeNode, sirenfield.ChildNodes);

                }
                else
                {
                    treeNode.PopulateOnDemand = sirenfield.TotalNodes > 0;

                    if (sirenfield.TotalNodes > 0)
                    {
                        treeNode.SelectAction = TreeNodeSelectAction.Expand;
                        treeNode.Collapse();
                    }
                    else
                        treeNode.SelectAction = TreeNodeSelectAction.Select;
                }
            }
        }

        private void PopulateChildNodes(TreeNode treeNode, List<SirenFieldInfo> lstSirenFields)
        {
            foreach (SirenFieldInfo sirenField in lstSirenFields)
            {
                TreeNode node = new TreeNode(sirenField.Caption, sirenField.ID + SEPERATOR.ToString() + sirenField.ParentID);

                treeNode.ChildNodes.Add(node);

                if (sirenField.ChildNodes.Count > 0)
                {
                    node.SelectAction = TreeNodeSelectAction.Expand;
                    node.Collapse();

                    PopulateChildNodes(node, sirenField.ChildNodes);
                }
                else
                {
                    node.PopulateOnDemand = sirenField.TotalNodes > 0;

                    if (sirenField.TotalNodes == 0)
                        node.SelectAction = TreeNodeSelectAction.Select;
                    else
                    {
                        node.SelectAction = TreeNodeSelectAction.Expand;
                        node.Collapse();
                    }

                }
            }
        }

        protected void tvSirenField_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (this.tvSirenField.SelectedNode.SelectAction != TreeNodeSelectAction.Select)
                return;

            string nodeValue = this.tvSirenField.SelectedNode.Value;

            if (!String.IsNullOrEmpty(nodeValue))
            {
                this.tvSirenField.SelectedNode.ChildNodes.Clear();

                string[] values = nodeValue.Split(SEPERATOR);

                //SirenFieldMgr sirenFieldMgr = new SirenFieldMgr(GetConnectionString);

                //List<SirenFieldInfo> lstSirenFieldsChildNode = sirenFieldMgr.GetChildSirenFields(values[ID_INDEX]);

                //PopulateChildNodes(this.tvSirenField.SelectedNode, lstSirenFieldsChildNode);

                Response.Write(nodeValue);
            }

            //this.tvSirenField.SelectedNode.Expanded = true;
            //this.tvSirenField.SelectedNode.PopulateOnDemand = false;

        }

        protected void tvSirenField_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            if (!Page.IsPostBack)
                return;

            if (e.Node.ChildNodes.Count > 0)
                return;

            string nodeValue = e.Node.Value;

            if (!String.IsNullOrEmpty(nodeValue))
            {
                string[] values = nodeValue.Split(SEPERATOR);

                SirenFieldMgr sirenFieldMgr = new SirenFieldMgr(GetConnectionString);

                List<SirenFieldInfo> lstSirenFieldsChildNode = sirenFieldMgr.GetChildSirenFields(values[ID_INDEX]);

                PopulateChildNodes(e.Node, lstSirenFieldsChildNode);
            }

            e.Node.Expanded = true;
            e.Node.PopulateOnDemand = false;
        }
    }
}